Telegram Group & Telegram Channel
🖥 Прочитать Nullable<long> из строки

Итак, дан код:

long? catCode = null;
// Если строка val не пустая,
if (!(String.IsNullOrWhiteSpace(val)))
{
// распознаем в первых цифрах до пробела целое 64-битное число.
// Если его там нет, записываем в catCode NULL.
// Если его удалось распознать, записываем его в catCode.
if (!(Int64.TryParse(val.Split(" ")[0], out long cc)))
{
catCode = null;
}
else
{
catCode = cc;
}
}

Как переписать этот код чуть понятнее?

Как вариант, можно написать что-то в духе:

public static string LeftDigits(this string str)
{
if (str == null) return null;
if (!str.contains(" ") return null;
string left = str.Split(" ")[0];
if (! left.All(Char.IsDigit){
return null;
}
return left;
}

//...

string digits = LeftDigits(val);
long? catCode = digits != null ? Int64.Parse(digits) : null;

А есть ли ещё варианты?
Можете накидать свои в комментах

@csharp_1001_notes
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/csharp_1001_notes/433
Create:
Last Update:

🖥 Прочитать Nullable<long> из строки

Итак, дан код:


long? catCode = null;
// Если строка val не пустая,
if (!(String.IsNullOrWhiteSpace(val)))
{
// распознаем в первых цифрах до пробела целое 64-битное число.
// Если его там нет, записываем в catCode NULL.
// Если его удалось распознать, записываем его в catCode.
if (!(Int64.TryParse(val.Split(" ")[0], out long cc)))
{
catCode = null;
}
else
{
catCode = cc;
}
}

Как переписать этот код чуть понятнее?

Как вариант, можно написать что-то в духе:

public static string LeftDigits(this string str)
{
if (str == null) return null;
if (!str.contains(" ") return null;
string left = str.Split(" ")[0];
if (! left.All(Char.IsDigit){
return null;
}
return left;
}

//...

string digits = LeftDigits(val);
long? catCode = digits != null ? Int64.Parse(digits) : null;

А есть ли ещё варианты?
Можете накидать свои в комментах

@csharp_1001_notes

BY C# 1001 notes




Share with your friend now:
tg-me.com/csharp_1001_notes/433

View MORE
Open in Telegram


C 1001 notes Telegram | DID YOU KNOW?

Date: |

Telegram auto-delete message, expiring invites, and more

elegram is updating its messaging app with options for auto-deleting messages, expiring invite links, and new unlimited groups, the company shared in a blog post. Much like Signal, Telegram received a burst of new users in the confusion over WhatsApp’s privacy policy and now the company is adopting features that were already part of its competitors’ apps, features which offer more security and privacy. Auto-deleting messages were already possible in Telegram’s encrypted Secret Chats, but this new update for iOS and Android adds the option to make messages disappear in any kind of chat. Auto-delete can be enabled inside of chats, and set to delete either 24 hours or seven days after messages are sent. Auto-delete won’t remove every message though; if a message was sent before the feature was turned on, it’ll stick around. Telegram’s competitors have had similar features: WhatsApp introduced a feature in 2020 and Signal has had disappearing messages since at least 2016.

What is Telegram?

Telegram’s stand out feature is its encryption scheme that keeps messages and media secure in transit. The scheme is known as MTProto and is based on 256-bit AES encryption, RSA encryption, and Diffie-Hellman key exchange. The result of this complicated and technical-sounding jargon? A messaging service that claims to keep your data safe.Why do we say claims? When dealing with security, you always want to leave room for scrutiny, and a few cryptography experts have criticized the system. Overall, any level of encryption is better than none, but a level of discretion should always be observed with any online connected system, even Telegram.

C 1001 notes from hk


Telegram C# 1001 notes
FROM USA